// 使用class类封装 会更有复用性
class WxRequest {
    // 定义实例属性 默认参数
    defaults = {
        baseURL: '', // 请求基准地址
        url: '',  // 接口的请求路径
        data: null,
        methods: 'GET',
        header: {
            'Content-type': 'application/json'
        },
        timeout: 6000,
        isLoading: true
    }
    // 定义拦截器对象
    interceptors = {
        request: (config) => {
            return config
        },
        response: (response) => {
            return response
        }
    }
    // 定义数组队列
    queue = []
    constructor(params = {}) {
        this.defaults = Object.assign({}, this.defaults, params)
    }
    request(options) {
        this.timeId && clearTimeout(this.timeId)
        options.url = this.defaults.baseURL+'/' + options.url
        options = { ...this.defaults, ...options }
        // wx.showLoading()
        if (options.isLoading && options.method !== 'UPLOAD') {
            this.queue.length === 0 && wx.showLoading()
            this.queue.push('request')
        }

        options = this.interceptors.request(options)
        return new Promise((resolve, reject) => {
            if (options.method === 'UPLOAD') {
                wx.uploadFile({
                    ...options,
                    success: (res) => {
                        res.data = JSON.parse(res.data)
                        const mergeRes = Object.assign({}, res, { config: options, isSuccess: true })
                        resolve(this.interceptors.response(mergeRes))
                    },
                    fail: (err) => {
                        const mergeErr = Object.assign({}, err, { config: options, isSuccess: false })
                        resolve(this.interceptors.response(mergeErr))
                    }
                })
            } else {
                wx.request({
                    ...options,
                    success: (res) => {
                        // 不管成功失败都要响应拦截器
                        const mergeRes = Object.assign({}, res, { config: options, isSuccess: true })
                        resolve(this.interceptors.response(mergeRes))
                    },
                    fail: (error) => {
                        const mergeErr = Object.assign({}, error, { config: options, isSuccess: false })
                        reject(this.interceptors.response(mergeErr))
                    },
                    complete: () => {
                        if (options.isLoading) {
                            // wx.hideLoading()
                            this.queue.pop()
                            this.queue.length === 0 && this.queue.push('request')
                            this.timeId = setTimeout(() => {
                                this.queue.pop()
                                this.queue.length === 0 && wx.hideLoading()
                                clearTimeout(this.timeId)
                            }, 1)
                        }
                    }
                })
            }
        })
    }
    // 封装请求方式
    // get
    get(url, data = {}, config = {}) {
        return this.request(Object.assign({ url, data, methods: 'GET' }, config))
    }
    // post
    post(url, data = {}, config = {}) {
        return this.request(Object.assign({ url, data, methods: 'POST' }, config))
    }
    // delet  e
    delete(url, data = {}, config = {}) {
        return this.request(Object.assign({ url, data, methods: 'DELETE' }, config))
    }
    // put
    put(url, data = {}, config = {}) {
        return this.request(Object.assign({ url, data, methods: 'PUT' }, config))
    }
    // all处理并发请求
    all(...promise) {
        return Promise.all(promise)
    }
    // upload进行封装
    upload(url, filePath, name = 'file', config = {}) {
        return this.request(Object.assign({ url, filePath, name, method: 'UPLOAD' }, config))
    }
}
export default WxRequest